package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sg.hms.Model.Department;
import com.sg.hms.Service.DepartmentService;
import com.sg.hms.Service.EmployeeService;
import com.sg.hms.ServiceImpl.DepartmentServiceImpl;
import com.sg.hms.ServiceImpl.EmployeeServiceImpl;
/**
 * 
 * @author lubinghan
 *
 */
public class ShowDepartmentController extends HttpServlet{
	//实例化一个部门服务（DepartmentServiceImpl）
	DepartmentService dService = new DepartmentServiceImpl();
	EmployeeService eService = new EmployeeServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();//实例化一个
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int pageStart = (page-1)*pageSize;
		
		//根据部门获得部门下的员工
		String dName = req.getParameter("searchName");
		String isDepart = req.getParameter("isDepart");
		if (dName!=null) {//判断是否搜索部门下的员工，用于页面加载时
			JSONArray depsEmployees = eService.selectEmployeeBydName(dName);
			pw.write(depsEmployees.toString());
		}else {//搜索所有的部门
			JSONObject departments = dService.selectDepartmentsBypage(pageStart,pageSize,dService.getAllDepartmentsSize());//获取所有部门
			pw.write(departments.toString());
		}
		
	}
}
